#include <msclr/marshal_cppstd.h>
#include "BulletEmitter.h"

using namespace bs;

namespace BulletScript
{

// ----------------------------------------------------------------------------
BulletEmitter::BulletEmitter(bs::Machine* machine, System::String^ definition, float x, float y, float angle)
	: mMachine(machine)
	, mActive(false)
	, mDisposed(false)
{
	mEmitter = mMachine->createEmitter(ConvertString(definition), x, y, angle, nullptr);
	if (mEmitter == 0)
		throw gcnew System::Exception("Could not create emitter named '" + definition + "'.");
}
// ----------------------------------------------------------------------------
BulletEmitter::~BulletEmitter()
{
	this->!BulletEmitter();
}
// ----------------------------------------------------------------------------
BulletEmitter::!BulletEmitter()
{
	if (!mDisposed)
	{
		mMachine->destroyEmitter(mEmitter);
		mDisposed = true;
	}
}
// ----------------------------------------------------------------------------
std::string BulletEmitter::ConvertString(System::String^ str)
{
	msclr::interop::marshal_context context;
	return context.marshal_as<std::string>(str);
}
// ----------------------------------------------------------------------------
void BulletEmitter::Activate()
{
	mActive = true;
}
// ----------------------------------------------------------------------------
void BulletEmitter::Deactivate()
{
	mActive = false;
}
// ----------------------------------------------------------------------------
bool BulletEmitter::isActive()
{
	return mActive;
}
// ----------------------------------------------------------------------------
void BulletEmitter::Update(float frameTime)
{
	mEmitter->update(frameTime);
}
// ----------------------------------------------------------------------------

}